//
//  MainTabBar.swift
//  Sina
//
//  Created by 邹志华 on 15/10/28.
//  Copyright © 2015年 zzh. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {
    
    private let cont : CGFloat = 5
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //计算宽度
        let width = bounds.width / cont
        
        //计算每个frame的大小
        let frame = CGRect(x: 0, y: 0, width: width, height: bounds.height)
//        print(frame)
        
        var index = 0
        for view in subviews
        {
//            print("View:\(view)")
            
            //is 是判断是否某个类型
            //as 是类型转换的意思
            if view is UIControl && !(view is UIButton)
            {
                
                view.frame =  CGRectOffset(frame, width * CGFloat(index), 0)
                index++
                if index == 2
                {
                    
                    index += index == 1 ? 2 : 1
                }
                
            }
        }
        
        //撰写按钮的frame
        composeButton.frame = CGRectOffset(frame, width * 2, 0  )
        
    }
    
    
//        MARK: -懒加载
        lazy var composeButton : UIButton = {
            
            
            let button = UIButton()
            
            //加号按钮图片
            button.setImage(UIImage (named:"tabbar_compose_icon_add"), forState:UIControlState.Normal)
             button.setImage(UIImage (named:"tabbar_compose_icon_add_highlighted"), forState:UIControlState.Highlighted)
            
            //加号按钮背景
            button.setBackgroundImage(UIImage(named: "tabbar_compose_button") , forState: UIControlState.Normal)
            
            button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted") , forState: UIControlState.Highlighted)
            
            self.addSubview(button)
            
            return button
        }()
    
    
    
    
    
    
    
    
}